package com.jblank.common.config;

import com.jblank.dto.DTO;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 保存配置
 * 
 * @author kim
 * @since 2013-09-28
 */
public class SaveConfig {
	public static final String insertOption = "insert";
	public static final String updateOption = "update";
	public static final String deleteOption = "delete";
	private String tableName;
	private String condExp;
	private JSONArray condValues;
	private JSONArray flds;
	private JSONArray fldTypes;
	private JSONArray values;
	private String option;

	public String getOption() {
		return option;
	}

	public void setOption(String option) {
		this.option = option;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getCondExp() {
		return condExp;
	}

	public void setCondExp(String condExp) {
		this.condExp = condExp;
	}

	public JSONArray getCondValues() {
		return condValues;
	}

	public void setCondValues(JSONArray condValues) {
		this.condValues = condValues;
	}

	public JSONArray getFlds() {
		return flds;
	}

	public void setFlds(JSONArray flds) {
		this.flds = flds;
	}

	public JSONArray getValues() {
		return values;
	}

	public void setValues(JSONArray values) {
		this.values = values;
	}
	
	public JSONArray getFldTypes() {
		return fldTypes;
	}

	public void setFldTypes(JSONArray fldTypes) {
		this.fldTypes = fldTypes;
	}
	/**
	 * dto解析为config
	 * @param dto
	 * @return SaveConfig
	 * 以后可改为反射
	 */
	public static SaveConfig transDtoToConfig(DTO dto){
		SaveConfig saveConfig = null;
		if(dto != null && dto.get(GlobalConfig.SaveConfig) != null){
			saveConfig = new SaveConfig();
			JSONObject saveJson = JSONObject.fromObject(dto.getAsString(GlobalConfig.SaveConfig));
			if(saveJson.has("tableName") && saveJson.getString("tableName") != null){
				saveConfig.setTableName(saveJson.getString("tableName"));
			}if(saveJson.has("condExp") && saveJson.getString("condExp") != null){
				saveConfig.setCondExp(saveJson.getString("condExp"));
			}if(saveJson.has("condValues") && saveJson.getString("condValues") != null){
				saveConfig.setCondValues(JSONArray.fromObject(saveJson.getString("condValues")));
			}if(saveJson.has("flds") && saveJson.getString("flds") != null){
				saveConfig.setFlds(JSONArray.fromObject(saveJson.getString("flds")));
			}if(saveJson.has("fldTypes") && saveJson.getString("fldTypes") != null){
				saveConfig.setFldTypes(JSONArray.fromObject(saveJson.getString("fldTypes")));
			}if(saveJson.has("values") && saveJson.getString("values") != null){
				saveConfig.setValues(JSONArray.fromObject(saveJson.getString("values")));
			}if(saveJson.has("option") && saveJson.getString("option") != null){
				saveConfig.setOption(saveJson.getString("option"));
			}
		}
		return saveConfig;
	}
}
